package com.vids.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.vids.constant.Urlconstant;
import com.vids.constant.VIIDRespEnum;
import com.vids.model.param.*;
import com.vids.model.response.ResponseStatus;
import com.vids.model.vo.SubImageInfoVo;
import com.vids.utils.BeanUtilss;
import com.vids.utils.ImageUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  通知功能 控制器
 * @author frh
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/VIID/SubscribeNotifications")
public class NotificationController
{
    /**
     *
     * 接受下级视图库推送的通知消息，处理，并应答通知消息。
     * 接受并解析通知消息，获取图片url，并根据url读取并保存图片。
     * 增加通知对象，实现向订阅发出方上报通知信息
     * 
     * @throws Exception
     */
    @PostMapping(produces = "application/VIID+JSON")
    @ApiOperation(value = "新增通知", httpMethod = "POST", notes = "新增通知")
    public Object createGatherNotification(@RequestBody SubscribeNotificationBatchParam param ) throws Exception
    {
//        log.info("Enter into #createGatherNotification #param:" + JSON.toJSONString(param));
        List<ResponseStatus> resps = new ArrayList<>();
        if (param == null || param.getSubscribeNotificationListObject() == null
                || param.getSubscribeNotificationListObject().getSubscribeNotificationObject() == null
                || param.getSubscribeNotificationListObject().getSubscribeNotificationObject().size() == 0)
        {
            ResponseStatus status = new ResponseStatus(Urlconstant.URL_SUBSCRIBE_NOTIFICATION,
                    VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空");
            resps.add(status);
            return new ResponseEntity<>(BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)), HttpStatus.CREATED);
        }

//        log.info("Notification getSubscribeNotificationObject().size ==" + param.getSubscribeNotificationListObject().getSubscribeNotificationObject().size());
//
//
//        log.info("Notification ========================> #param:" + JSON.toJSONString(param));


        List<SubscribeNotificationModel> list = param.getSubscribeNotificationListObject()
                .getSubscribeNotificationObject();

        for (SubscribeNotificationModel snm : list)
        {

            //机动车
            MotorVehicleBatchModel motorVehicleObjectList = snm.getMotorVehicleObjectList();
            if (motorVehicleObjectList != null) {
                log.info("订阅通知：机动车保存开始");
                List<MotorVehicleModel> motorVehicleObject = motorVehicleObjectList.getMotorVehicleObject();
                if (CollUtil.isNotEmpty(motorVehicleObject)) {
                        //TODO:这里把数据接到公司对应的接口
                    //TransactionReceipt transactionReceipt = carCallbackMock.getResult();   // 部署合约的回执信息
                    for (MotorVehicleModel motorVehicleModel: motorVehicleObject)
                    {
                        List<SubImageInfoVo> subImageInfoObject = motorVehicleModel.getSubImageList().getSubImageInfoObject();
                        for (SubImageInfoVo infoVo: subImageInfoObject)
                        {
                            log.info("RENStorage Path=======: " + infoVo.getStoragePath());

                            if (infoVo.getType().contains("01"))
                            {
                                ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
                                        motorVehicleModel.getMotorVehicleID(), "motor");
                            }else if(infoVo.getType().contains("02"))
                            {
                                ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
                                        motorVehicleModel.getMotorVehicleID()+"_plate", "motor");
                            }else {
                                ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
                                        motorVehicleModel.getMotorVehicleID()+"_other", "motor");
                            }

//                            ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
//                                                              infoVo.getImageID(),"motor");
                        }
                    }
                }
//                log.info("订阅通知：机动车保存结束");
            }

            //人脸
            FaceBatchModel faceObjectList = snm.getFaceObjectList();
            if (faceObjectList != null) {
                log.info("订阅通知：场景和人脸保存开始");
                List<FaceModel> faceObject = faceObjectList.getFaceObject();
                if (CollUtil.isNotEmpty(faceObject))
                {
                    for (FaceModel faceModel: faceObject)
                    {
                        List<SubImageInfoVo> subImageInfoObject = faceModel.getSubImageList().getSubImageInfoObject();
                        for (SubImageInfoVo infoVo: subImageInfoObject)
                        {
                            log.info("REStorage Path=======: " + infoVo.getStoragePath());
                            if (infoVo.getType().contains("11"))
                            {
                                ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
                                        faceModel.getFaceID()+"_face","face");
                            }else if (infoVo.getType().contains("14"))
                            {
                                ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
                                        faceModel.getFaceID()+"_faceBackgroundImage","face");
                            } else {
                                ImageUtil.getImageFromViidAndSave(infoVo.getStoragePath(), infoVo.getDeviceID(),
                                        faceModel.getFaceID()+"_other","face");
                            }
                        }

                    }
                }
//                log.info("订阅通知：场景和人脸保存结束");
            }

            //人体
            PersonBatchModel personObjectList = snm.getPersonObjectList();
            if (personObjectList != null) {
                List<PersonModel> personObject = personObjectList.getPersonObject();
                if (CollUtil.isNotEmpty(personObject)) {
                        //TODO:这里把数据接到公司对应的接口
                }
            }

            ResponseStatus status = new ResponseStatus(Urlconstant.URL_SUBSCRIBE_NOTIFICATION,
                    VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.OK, "请求通知接口成功");
            resps.add(status);
            return new ResponseEntity<>(BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)),
                    HttpStatus.CREATED);
        }
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_SUBSCRIBE_NOTIFICATION,
                VIIDRespEnum.OK.getCode(), new Date());
        setStatus(status, VIIDRespEnum.OK, "请求通知接口成功");
        resps.add(status);
        return new ResponseEntity<>(BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)),
                HttpStatus.CREATED);

    }



    protected void setStatus(ResponseStatus res, VIIDRespEnum e, String field) {
        StringBuffer sb = new StringBuffer();
        sb.append(e.getMsg() + "[" + field + "]");
        res.setStatusCode(e.getCode());
        res.setStatusString(sb.toString());
    }
}
